package com.example.test.rabbit;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    /**
     * 本服务器sid=9527 , 本服务器隶属单位unitid=10086
     * 声明交换机
     * 1.单位交换机(单位广播)
     * 2.公共交换机(通过routingKey点对点发消息)
     **/
    @Bean
    public FanoutExchange unitExchange() {
        return new FanoutExchange("fanout.exchange.unit_10086", true, false);
    }

    @Bean
    public DirectExchange commonExchange() {
        return new DirectExchange("direct.exchange.common", true, false);
    }

    /**
     * 声明队列
     * 1.本服务器消费队列
     **/
    @Bean
    public Queue localQueue() {
        return new Queue("queue.sid_9527", true, false, false);
    }

    /**
     * 声明绑定关系
     * 1.单位交换机-本服务器消费队列(不需要routingKey)
     * 2.公共交换机-本服务器消费队列(需要routingKey)
     **/
    @Bean
    public Binding unitBinding() {
        return BindingBuilder.bind(localQueue()).to(unitExchange());
    }

    @Bean
    public Binding commonBinding() {
        return BindingBuilder.bind(localQueue()).to(commonExchange()).with("sid_9527");
    }
}
